package com.zx.controller;

import com.zx.common.ResponseCode;
import com.zx.entity.Category;
import com.zx.repository.CategoryRepository;
import com.zx.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryRepository repository;
    @Autowired
    private CategoryService service;

    @GetMapping("/addCategory")
    public ModelAndView addCategory(){
        return new ModelAndView("category/addCategory");
    }
    @PostMapping("/addCategory")
    public ModelAndView addCategory(@RequestParam("categoryName") String categoryName,
                                    Category category,
                                    Map<String,Object> map){
        Category cate = service.findCategoryInfoByCategoryName(categoryName);
        if(cate!=null){
            map.put("msg",ResponseCode.Category_EXISTS.getDesc());
            map.put("url","/foodshare/category/list");
            return new ModelAndView("common/error",map);
        }
        repository.save(category);
        map.put("msg",ResponseCode.ADD_SUCCESS.getDesc());
        map.put("url","/foodshare/category/list");
        return new ModelAndView("common/success");
    }
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "0") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size,
                             Map<String,Object> map){
        PageRequest request = PageRequest.of(page,size);
        Page<Category> category = repository.findAll(request);
        map.put("category",category);
        return new ModelAndView("category/list");
    }
    @GetMapping("/list/{categoryId}")
    public ModelAndView EditUser(@PathVariable("categoryId") Integer id,
                                 Map<String,Object> map){
        Category cate = repository.getOne(id);
        map.put("category",cate);
        return new ModelAndView("category/editCategory",map);
    }
    @GetMapping("/delete/{categoryId}")
    public ModelAndView delete(@PathVariable("categoryId") Integer id,
                               Map<String,Object> map){
        Category category = repository.getOne(id);
        repository.delete(category);
        map.put("msg",ResponseCode.DELETE_SUCCESS.getDesc());
        map.put("url","/foodshare/category/list");
        return new ModelAndView("common/success",map);
    }
    @PostMapping("/saveCategory")
    public ModelAndView saveCategory(Category category,
                                     Map<String,Object> map){
        repository.save(category);
        map.put("msg",ResponseCode.EDIT_SUCCESS.getDesc());
        map.put("url","/foodshare/category/list");
        return new ModelAndView("common/success",map);
    }
}
